package com.bckj.freshnow.goods.mapper;

import com.bckj.freshnow.goods.entity.GoodsSku;
import com.bckj.freshnow.goods.model.GoodsSkuResponse;
import com.bckj.freshnow.goods.model.GoodsSkuRequest;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.ibatis.annotations.Mapper;

/**
 * 商品SKU表(GoodsSku)表数据库访问层
 *
 * @author makejava
 * @since 2025-04-21 20:10:18
 */
@Mapper
public interface GoodsSkuMapper extends BaseMapper<GoodsSku> {

    /**
      * 列表
      *
      * @param request req
      * @return list
      */
    @SelectProvider(method = "findAll" , type = GoodsSkuProvider.class)
    List<GoodsSkuResponse> findAll(GoodsSkuRequest request);

    @SelectProvider(method = "findBySkuCodes" , type = GoodsSkuProvider.class)
    List<GoodsSkuResponse> findBySkuCodes(@Param("skuCodes") List<String> skuCodes);

    class GoodsSkuProvider{
        
        public String findAll(GoodsSkuRequest request){
            SQL sql = new SQL();
            sql.SELECT("gs.id, gs.goods_id, gs.sku_code, gs.price, gs.origin_price, gs.stock, gs.stock_warn, gs.weight, gs.image_url, gs.status, gs.spec, gs.create_time, gs.update_time");
            sql.FROM("goods_sku gs");
            return sql.toString();
        }

        public String findBySkuCodes(List<String> skuCodes){
            SQL sql = new SQL();
            sql.SELECT("gs.id, gs.goods_id, gs.sku_code, gs.price, gs.origin_price, gs.stock, gs.stock_warn, gs.weight, gs.image_url, gs.status, gs.spec, gs.create_time, gs.update_time");
            sql.SELECT("g.goods_name, g.goods_code, g.goods_image, g.goods_desc");
            sql.FROM("goods_sku gs");
            sql.LEFT_OUTER_JOIN("goods g on gs.goods_id = g.id");
            sql.WHERE("gs.sku_code in (" + String.join(",", skuCodes) + ")");
            return sql.toString();
        }
    }
}

